package com.desmart.renjiao.main.workplace;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.desmart.renjiao.R;
import com.desmart.renjiao.contants.Constants;
import com.desmart.renjiao.main.adapter.HomeItemType;
import com.desmart.renjiao.main.adapter.HomeTypeAdapter;
import com.desmart.renjiao.main.bean.EmptyLine;
import com.desmart.renjiao.main.bean.HomeIcons;
import com.desmart.renjiao.main.bean.Title;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonFragment;
import com.lemon.library.common.router.JumpUtils;
import com.lemon.library.utils.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

@Route(path = ARoutePath.Main.WorkPlaceFragment)
public class WorkPlaceFragment extends CommonFragment<WorkPlaceM,WorkPlaceP> implements HomeTypeAdapter.OnItemClick,WorkPlace.WorkPlaceV{

    private RecyclerView recyclerview;
    private HomeTypeAdapter mAdapter;
    private String key;

    private static ArrayList<HomeIcons.Icon> zhuyelist = new ArrayList<>();
    private static ArrayList<HomeIcons.Icon> shequlist = new ArrayList<>();
    private static ArrayList<HomeIcons.Icon> oalist = new ArrayList<>();

    static {
        zhuyelist.add(new HomeIcons.Icon(R.string.kaoqin,R.drawable.icon_workplace_kaoqing));
        zhuyelist.add(new HomeIcons.Icon(R.string.danfeichaxun,R.drawable.icon_danfeichaxun));
        zhuyelist.add(new HomeIcons.Icon(R.string.gerenwangpan,R.drawable.icon_workplace_wangpan));
        zhuyelist.add(new HomeIcons.Icon(R.string.jiaodianluntan,R.drawable.icon_jiaodianluntan));
        zhuyelist.add(new HomeIcons.Icon(R.string.xinxiziliaoku,R.drawable.icon_workplace_xinxiziliao));
        zhuyelist.add(new HomeIcons.Icon(R.string.buchongyiliaochaxun,R.drawable.icon_buchongyiliaochaxun));
        zhuyelist.add(new HomeIcons.Icon(R.string.gerengongzichaxun,R.drawable.icon_home_yuangongzizhu));
        zhuyelist.add(new HomeIcons.Icon(R.string.cankaxinxichaxun,R.drawable.icon_home_yuangongzizhu));

        shequlist.add(new HomeIcons.Icon(R.string.tushuguan,R.drawable.icon_workplace_tushuguan));
        shequlist.add(new HomeIcons.Icon(R.string.jiaocaiyijianfankui,R.drawable.icon_workplace_jiaocaiyijianfankui));
        shequlist.add(new HomeIcons.Icon(R.string.bianjishijie,R.drawable.icon_workplace_bianjishijie));
        shequlist.add(new HomeIcons.Icon(R.string.shenduzhichuan,R.drawable.icon_workplace_shenduzhichuan));
        shequlist.add(new HomeIcons.Icon(R.string.keyantiandi,R.drawable.icon_home_keyantiandi));
        shequlist.add(new HomeIcons.Icon(R.string.peixungongzuoguanli,R.drawable.icon_peixunguanli));
        shequlist.add(new HomeIcons.Icon(R.string.danqunpingtai,R.drawable.icon_danqunpingtai));
        shequlist.add(new HomeIcons.Icon(R.string.sheban,R.drawable.icon_workplace_shequ));


        oalist.add(new HomeIcons.Icon(R.string.gudingzichangguanli,R.drawable.icon_workplace_gudingzichangguanli));
        oalist.add(new HomeIcons.Icon(R.string.huiyiguanli,R.drawable.icon_oa_huiyiguanli));
        oalist.add(new HomeIcons.Icon(R.string.peixungongzuoguanli,R.drawable.icon_oa_peixungongzuoguanli));
        oalist.add(new HomeIcons.Icon(R.string.renziliucheng,R.drawable.icon_renziliucheng));

    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_main_work_place;
    }

    @Override
    public void initView(View view) {

        SmartRefreshLayout refreshLayout = view.findViewById(R.id.refreshlayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setDragRate(0.3f);
        refreshLayout.setEnableOverScrollBounce(true);
        refreshLayout.setEnableOverScrollDrag(true);

        recyclerview = view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new HomeTypeAdapter(getPlaceHolderData(),4);
        recyclerview.setAdapter(mAdapter);

        mAdapter.setOnItemClick(this);
    }

    public List<HomeItemType> getPlaceHolderData() {
        List<HomeItemType> list = new ArrayList<>();
        list.add(new Title(getString(R.string.bangongchangyong)));
        list.add(new HomeIcons(zhuyelist));
        list.add(new EmptyLine());
        list.add(new Title(getString(R.string.shequ)));
        list.add(new HomeIcons(shequlist));
        list.add(new EmptyLine());
        list.add(new Title(getString(R.string.oaliucheng)));
        list.add(new HomeIcons(oalist));
        return list;
    }

    @Override
    public void initData() {

    }

    @Override
    public void onItemClick(int title) {
        if (title == R.string.danfeichaxun){
            mPresenter.getEmployeesPort(title);
        }else if (title == R.string.buchongyiliaochaxun){
            mPresenter.getEmployeesPort(title);
        }else if (title == R.string.gerengongzichaxun){
            mPresenter.getEmployeesPort(title);
        }else if (title == R.string.cankaxinxichaxun){
            mPresenter.getEmployeesPort(title);
        }
    }

    @Override
    public void onSuccessGetKey(String key, int title) {
        this.key = key;
        switch (title){
            case R.string.danfeichaxun:
                JumpWebEmployee(Constants.employeeself_4,getString(R.string.danfeichaxun));
                break;
            case R.string.buchongyiliaochaxun:
                JumpWebEmployee(Constants.employeeself_6,getString(R.string.buchongyiliaochaxun));
                break;
            case R.string.gerengongzichaxun:
                JumpWebEmployee(Constants.employeeself_2,getString(R.string.gerengongzichaxun));
                break;
            case R.string.cankaxinxichaxun:
                JumpWebEmployee(Constants.employeeself_3,getString(R.string.cankaxinxichaxun));
                break;
        }
    }

    private void JumpWebEmployee(String Id,String name) {
        String url = Constants.EmployeeSelfUrl + Id + "&NavMode=1&CurrentWindowId=&TarTitle="
                + name + "&sso_ticket=" + key;
        JumpUtils.toWeb(getActivity(),url,name);
    }

}
